summaryrefslogtreecommitdiff
path: root/app/[lng]/evcp/data-room/[projectId]/stats/page.tsx
blob: 7f652a99d21124e68c08145b09ee63bb8b480892 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// app/projects/[projectId]/stats/page.tsx
'use client';

import { use, useState, useEffect } from 'react';
import { 
  BarChart3, 
  TrendingUp, 
  HardDrive, 
  Users, 
  Eye, 
  Download,
  Upload,
  Calendar,
  FileText,
  FolderOpen,
  Activity
} from 'lucide-react';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { Badge } from '@/components/ui/badge';
import { Progress } from '@/components/ui/progress';
import { useToast } from '@/hooks/use-toast';
import { cn } from '@/lib/utils';

interface ProjectStats {
  storage: {
    used: number;
    limit: number;
    fileCount: number;
    folderCount: number;
    byCategory: {
      public: number;
      restricted: number;
      confidential: number;
      internal: number;
    };
  };
  activity: {
    views: number;
    downloads: number;
    uploads: number;
    shares: number;
    trend: number; // 증감률
  };
  users: {
    total: number;
    active: number;
    byRole: {
      admin: number;
      editor: number;
      viewer: number;
    };
  };
  recent: {
    type: string;
    user: string;
    action: string;
    timestamp: string;
    details: string;
  }[];
}

export default function ProjectStatsPage({ 
  params 
}: { 
  params: Promise<{ projectId: string }> 
}) {
  // Next.js 15에서 params를 unwrap
  const resolvedParams = use(params);
  const projectId = resolvedParams.projectId;
  
  const [stats, setStats] = useState<ProjectStats | null>(null);
  const [loading, setLoading] = useState(true);
  const [dateRange, setDateRange] = useState('30d');
  const { toast } = useToast();

  useEffect(() => {
    fetchStats();
  }, [projectId, dateRange]);

  const fetchStats = async () => {
    try {
      setLoading(true);
      const response = await fetch(
        `/api/projects/${projectId}/stats?range=${dateRange}`
      );
      
      if (!response.ok) {
        if (response.status === 403) {
          throw new Error('통계를 볼 권한이 없습니다');
        }
        throw new Error('통계 로드 실패');
      }
      
      const data = await response.json();
      setStats(data);
    } catch (error: any) {
      toast({
        title: '오류',
        description: error.message || '통계를 불러올 수 없습니다.',
        variant: 'destructive',
      });
    } finally {
      setLoading(false);
    }
  };

  const formatBytes = (bytes: number) => {
    if (bytes === 0) return '0 Bytes';
    const k = 1024;
    const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
  };

  const formatNumber = (num: number) => {
    return new Intl.NumberFormat('ko-KR').format(num);
  };

  if (loading) {
    return (
      <div className="p-6">
        <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
          {[...Array(8)].map((_, i) => (
            <div key={i} className="h-32 bg-gray-200 animate-pulse rounded-lg" />
          ))}
        </div>
      </div>
    );
  }

  if (!stats) {
    return (
      <div className="p-6 text-center">
        <BarChart3 className="h-12 w-12 mx-auto mb-3 text-muted-foreground" />
        <p className="text-muted-foreground">통계를 불러올 수 없습니다</p>
      </div>
    );
  }

  const storagePercentage = (stats.storage.used / stats.storage.limit) * 100;

  return (
    <div className="p-6 space-y-6">
      {/* 헤더 */}
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-bold">프로젝트 통계</h1>
          <p className="text-muted-foreground mt-1">
            프로젝트 사용 현황과 활동 내역을 확인합니다
          </p>
        </div>
        
        <Tabs value={dateRange} onValueChange={setDateRange}>
          <TabsList>
            <TabsTrigger value="7d">7일</TabsTrigger>
            <TabsTrigger value="30d">30일</TabsTrigger>
            <TabsTrigger value="90d">90일</TabsTrigger>
          </TabsList>
        </Tabs>
      </div>

      {/* 주요 지표 */}
      <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
        <Card>
          <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
            <CardTitle className="text-sm font-medium">스토리지 사용량</CardTitle>
            <HardDrive className="h-4 w-4 text-muted-foreground" />
          </CardHeader>
          <CardContent>
            <div className="text-2xl font-bold">
              {formatBytes(stats.storage.used)}
            </div>
            {/* <Progress value={storagePercentage} className="mt-2" /> */}
            {/* <p className="text-xs text-muted-foreground mt-1">
              총 {formatBytes(stats.storage.limit)} 중 {storagePercentage.toFixed(1)}% 사용
            </p> */}
          </CardContent>
        </Card>

        <Card>
          <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
            <CardTitle className="text-sm font-medium">파일 수</CardTitle>
            <FileText className="h-4 w-4 text-muted-foreground" />
          </CardHeader>
          <CardContent>
            <div className="text-2xl font-bold">
              {formatNumber(stats.storage.fileCount)}
            </div>
            <p className="text-xs text-muted-foreground mt-1">
              폴더 {formatNumber(stats.storage.folderCount)}개 포함
            </p>
          </CardContent>
        </Card>

        <Card>
          <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
            <CardTitle className="text-sm font-medium">활성 사용자</CardTitle>
            <Users className="h-4 w-4 text-muted-foreground" />
          </CardHeader>
          <CardContent>
            <div className="text-2xl font-bold">
              {stats.users.active}
            </div>
            <p className="text-xs text-muted-foreground mt-1">
              전체 {stats.users.total}명 중
            </p>
          </CardContent>
        </Card>

        <Card>
          <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
            <CardTitle className="text-sm font-medium">총 다운로드</CardTitle>
            <Download className="h-4 w-4 text-muted-foreground" />
          </CardHeader>
          <CardContent>
            <div className="text-2xl font-bold">
              {formatNumber(stats.activity.downloads)}
            </div>
            <div className="flex items-center gap-1 mt-1">
              {stats.activity.trend > 0 ? (
                <TrendingUp className="h-3 w-3 text-green-500" />
              ) : (
                <TrendingUp className="h-3 w-3 text-red-500 rotate-180" />
              )}
              <span className={cn(
                "text-xs",
                stats.activity.trend > 0 ? "text-green-500" : "text-red-500"
              )}>
                {Math.abs(stats.activity.trend)}%
              </span>
            </div>
          </CardContent>
        </Card>
      </div>

      {/* 상세 통계 */}
      <div className="grid gap-6 md:grid-cols-2">
        {/* 파일 카테고리 분포 */}
        <Card>
          <CardHeader>
            <CardTitle>파일 카테고리</CardTitle>
            <CardDescription>카테고리별 파일 분포</CardDescription>
          </CardHeader>
          <CardContent className="space-y-3">
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                <div className="h-2 w-2 bg-green-500 rounded-full" />
                <span className="text-sm">Public</span>
              </div>
              <span className="text-sm font-medium">
                {stats.storage.byCategory.public}
              </span>
            </div>
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                <div className="h-2 w-2 bg-yellow-500 rounded-full" />
                <span className="text-sm">Restricted</span>
              </div>
              <span className="text-sm font-medium">
                {stats.storage.byCategory.restricted}
              </span>
            </div>
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                <div className="h-2 w-2 bg-red-500 rounded-full" />
                <span className="text-sm">Confidential</span>
              </div>
              <span className="text-sm font-medium">
                {stats.storage.byCategory.confidential}
              </span>
            </div>
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                <div className="h-2 w-2 bg-blue-500 rounded-full" />
                <span className="text-sm">Internal</span>
              </div>
              <span className="text-sm font-medium">
                {stats.storage.byCategory.internal}
              </span>
            </div>
          </CardContent>
        </Card>

        {/* 활동 요약 */}
        <Card>
          <CardHeader>
            <CardTitle>활동 요약</CardTitle>
            <CardDescription>기간별 활동 내역</CardDescription>
          </CardHeader>
          <CardContent className="space-y-3">
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                <Eye className="h-4 w-4 text-muted-foreground" />
                <span className="text-sm">조회수</span>
              </div>
              <span className="text-sm font-medium">
                {formatNumber(stats.activity.views)}
              </span>
            </div>
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                <Download className="h-4 w-4 text-muted-foreground" />
                <span className="text-sm">다운로드</span>
              </div>
              <span className="text-sm font-medium">
                {formatNumber(stats.activity.downloads)}
              </span>
            </div>
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                <Upload className="h-4 w-4 text-muted-foreground" />
                <span className="text-sm">업로드</span>
              </div>
              <span className="text-sm font-medium">
                {formatNumber(stats.activity.uploads)}
              </span>
            </div>
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                <Users className="h-4 w-4 text-muted-foreground" />
                <span className="text-sm">공유</span>
              </div>
              <span className="text-sm font-medium">
                {formatNumber(stats.activity.shares)}
              </span>
            </div>
          </CardContent>
        </Card>
      </div>

{/* 최근 활동 */}
<Card>
  <CardHeader>
    <CardTitle>최근 활동</CardTitle>
    <CardDescription>프로젝트 내 최근 활동 내역</CardDescription>
  </CardHeader>

  {/* 패딩이 스크롤에 포함되도록 CardContent p-0 + 내부 래퍼에 패딩 */}
  <CardContent className="p-0">
    <div
      className="max-h-80 md:max-h-96 xl:max-h-[480px] overflow-y-auto px-6 pb-6"
      style={{ scrollbarGutter: "stable" }} // 스크롤바 생겨도 레이아웃 흔들림 방지
      aria-label="최근 활동 스크롤 영역"
      tabIndex={0} // 키보드 포커스 가능
    >
      <ul role="list" className="divide-y">
        {stats.recent.map((activity, index) => (
          <li key={index} className="flex items-center gap-3 py-3">
            <Activity className="h-4 w-4 text-muted-foreground shrink-0" />
            <div className="min-w-0 flex-1">
              <p className="text-sm">
                <span className="font-medium">{activity.user}</span>
                {" "}님이{" "}
                <span className="font-medium">{activity.details}</span>
                {activity.action === "upload" && "을(를) 업로드했습니다"}
                {activity.action === "download" && "을(를) 다운로드했습니다"}
                {activity.action === "view" && "을(를) 조회했습니다"}
                {activity.action === "share" && "을(를) 공유했습니다"}
              </p>
              <p className="text-xs text-muted-foreground mt-1">
                {new Date(activity.timestamp).toLocaleString()}
              </p>
            </div>
          </li>
        ))}
      </ul>
    </div>
  </CardContent>
</Card>
    </div>
  );
}